import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login.vue'
import Welocome from '../views/welcome-block.vue'
import Home from '../views/Home.vue'
import UserList from "../components/Home/aside/user_list.vue"
import Calendar_Home from "../components/Home/aside/calendar_home.vue"
import Temperature from "../components/Home/aside/temperature.vue"
import Not_found from "../views/404.vue"
import Humidity from "../components/Home/aside/humidity.vue"
Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [{
      path: '/',
      component: Welocome
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect:'/calendar_home',
      children:[{
        path:'/user_list',
        component:UserList
      },{
        path:'/temperature',
        component:Temperature
      },{
        path:'/calendar_home',
        component:Calendar_Home
      },{
        path:'/humidity',
        component:Humidity
      }],
    },
    {
      path:'*',
      component:Not_found
    },
  ]
});

//利用路由导航守卫来进行控制访问权限
//如果用户没有登录，但是可以直接通过URL访问特定页面，需要重新导航到登录界面
router.beforeEach((to, from, next) => {
  //如果用户访问登录页或者主页，直接放行
  if (to.path === '/login' || to.path === '/') {
    return next();
  }
  //从sesssionStorage中获取到保存的token值
  const tokenStr = window.sessionStorage.getItem('token');
  //没有token，强制跳转
  if (!tokenStr) {
    return next('/login');
  }
  next();
})
export default router;
